package com.ima.web.service.impl;

import java.util.Date;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ima.web.beans.db.Categories;
import com.ima.web.beans.db.Stories;
import com.ima.web.beans.db.SubCategories;
import com.ima.web.beans.form.StoriesForm;
import com.ima.web.dao.StoriesDao;
import com.ima.web.service.CategoriesService;
import com.ima.web.service.StoriesService;
import com.ima.web.service.SubCategoriesService;
import com.ima.web.service.UserService;

@Service
@Transactional
public class StoriesServiceImpl implements StoriesService{

	private CategoriesService categoriesService;
	private SubCategoriesService subCategoriesService;
	private UserService userService;
	
	private StoriesDao storiesDao;
	
	public void setDependencies(CategoriesService categoriesService,
								SubCategoriesService subCategoriesService,
								UserService userService,
								StoriesDao storiesDao){
		this.categoriesService = categoriesService;
		this.subCategoriesService = subCategoriesService;
		this.userService = userService;
		this.storiesDao = storiesDao;
	}
	
	@Override
	public void createStory(StoriesForm s) {
		Stories story = new Stories();
		
		Categories c = categoriesService.getById(s.getCategoriesId());
		SubCategories sc = subCategoriesService.getById(s.getSubCategoriesId());
		
		story.setCategory(c);
		story.setSubCategory(sc);
		story.setTitle(s.getTitle());
		story.setSummary(s.getSummary());
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		story.setUser(userService.getUserData(username));
		
		Date now = new Date();
		
		story.setCreatedDate(now);
		story.setUpdatedDate(now);
		
		storiesDao.save(story);
	}

	@Override
	public void createStory(Stories s) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteStory(StoriesForm s) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteStory(Stories s) {
		// TODO Auto-generated method stub
		
	}

}
